<?php
  class newpage extends admin{
    function make_extra($buff,&$buff){
      $_SESSION['admin_title'.$_GET['file']] = L_CREATE_PAGE;

      if(isset($_GET['id'])){
	$id = $_GET['id'];
	$last_id = (int)$_GET['id'];
      } else {
	$last_id = $this->get_last_id();
      }

      if($id){
	$edit_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."blog WHERE id = $id AND type = 2");
	$this->row = $this->fetch_array($edit_query);
	
      } else {
	$edit_id = 0;
      }

      if(isset($_POST['status'])){
		if(empty($_REQUEST['expires'])){
			$expires = '2033-01-01 00:00:00';
		} else {
			$expires = $_REQUEST['expires'];
		}

		if(empty($_REQUEST['display'])){
			$display = date("Y-m-d H:i:s");
		} else {
			$display = $_POST['display'];
		}

		$status = $_POST['status'];
		$page_title = utf8_decode($_POST['page_title']);
		$page_content = utf8_decode($_POST['page_content']);
		$posted = date("Y-m-d H:i:s");
		$allow_comments = $_POST['allow_comments'];
		$aid = $_SESSION['uid'];

		if(!isset($_POST['page_parent'])){
			$parent = 0;
		} else {
			$parent = $_POST['page_parent'];
		}

		if($last_id == $this->get_last_id()){
			$query = $this->query("INSERT INTO `".KLEO_TABLE_PREFIX."blog` (
				`id` ,
				`title` ,
				`footer` ,
				`icon` ,
				`excerpt` ,
				`content` ,
				`expires` ,
				`posted` ,
				`display` ,
				`password` ,
				`allow_comments` ,
				`views` ,
				`type` ,
				`aid` ,
				`parent`,
				`home` ,
				`status`
			) VALUES (
				NULL , '$page_title', '', 'default.png', '', '$page_content', '$expires', '$posted', '$display' , '$password', '$allow_comments', '1', '2', '$aid', '$parent', '1', '$status'
			);");
		  if(!isset($_SESSION['dashboard_status'])){
		    $_SESSION['dashboard_status'] = array(0,L_CHANGED_SUCCESFULLY);
		  }
		} else {
			$this->query("UPDATE `".KLEO_TABLE_PREFIX."blog` SET 
		   	`title` = '$page_title',
		   	`content` = '$page_content',
		   	`expires` = '$expires',
		   	`posted` = '$posted',
		   	`display` = '$display',
		   	`password` = '$password',
		   	`parent` = '$parent',
		   	`allow_comments` = '$allow_comments',
		   	`status` = '$status'
			WHERE `".KLEO_TABLE_PREFIX."blog`.`id` =$last_id LIMIT 1 ;");
	if(!isset($_SESSION['dashboard_status'])){
	 	$_SESSION['dashboard_status'] = array(0,L_CHANGED_SUCCESFULLY);
	}
		}
      }
      $list_pages = $this->get_page_parents();

      $this->tpl_set(array(
	"PAGE_TITLE" => utf8_encode($this->row['title']),
	"PAGE_CONTENT" => utf8_encode($this->row['content']),
	"POST_PASSWORD" => $this->row['password'],
	"POST_EXPIRES" => $this->row['expires'],
	"POST_START" => $this->row['display'],
	"LAST_ID" => $last_id,
	"LIST_PAGES" => $list_pages
      ), false, $buff, $buff);
    }
    function get_page_parents($name='page_parent',$class='field-form-2'){
      if(isset($_GET['id'])){
	$id = $_GET['id'];
      } else {
	$id = 0;
      }
      $pages_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."blog WHERE type = 2 AND status = 1 AND id != $id ORDER BY title DESC");
      if($this->num_rows($pages_query) !== 0){
	$list_pages = '<select class="'.$class.'" name="'.$name.'">';
	$list_pages .= '<option value="0" '.$selected.'>'.L_NONE.'</option>';
	while($pages_row = $this->fetch_array($pages_query)){
	  if($this->row['parent'] == $pages_row['id']){
	    $selected = 'selected';
	  }
	  $list_pages .= '<option value="'.$pages_row['id'].'" '.$selected.'>'.$pages_row['title'].'</option>';
	  unset($selected);
	}
	$list_pages .= '</select>';
      } else {
	$list_pages = L_NO_PAGES_TO_PARENT;
      }
      return $list_pages;
  }
}